package com.java.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.java.entity.ExAccount;
import com.java.entity.ExChapter;
import com.java.entity.ExChapterRecord;
import com.java.entity.vo.ChapterRecordVo;
import com.java.entity.vo.ExTopicVo;
import com.java.entity.vo.SubmitRecord;
import com.java.service.AccountService;
import com.java.service.ChapterService;
import com.java.service.TopicService;
import com.java.util.MyCons;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;
import com.java.util.servlet.ServletUtil;

@RestController
@RequestMapping("/user")
public class UserController {
	@Autowired private AccountService accountService;
	@Autowired private ChapterService chapterService;
	@Autowired private TopicService topicService;
	
	
	@PostMapping("/logon")
	public ExAccount logon(
			@RequestParam(required = true) String openId) {
		return accountService.getAccountByOpenId(openId);
	}
	
	
	@PostMapping("/getAccount")
	public ExAccount getAccount(
			@RequestParam(required = true) String accountId) {
		return accountService.getAccount(accountId);
	}
	
	@PostMapping("/saveAccount")
	public ExAccount saveAccount(
			@RequestParam(required = true) String accountNick,
			@RequestParam(required = true) String accountLogo) {
		return null;
	}
	
	
	@PostMapping("/getTopicVo")
	public ExTopicVo getTopicVo(
			@RequestParam(required = true) String topicId) {
		return topicService.getTopicVo(topicId);
	}
	
	@PostMapping("/getRecordVo")
	public BaseRespone getRecordVo(
			@RequestParam(required = true) String chapterId,
			@RequestParam(required = true) String accountId) {
		ChapterRecordVo info = chapterService.getChapterRecordVo(chapterId, accountId);
		return BaseRespone.success(info);
	}
	
	@PostMapping("/initRecord")
	public BaseRespone initRecord(
			@RequestParam(required = true) String chapterId,
			@RequestParam(required = true) String accountId) {
		Object user = ServletUtil.getSession().getAttribute(MyCons.USER_ROLE);
		if(null == user) {
			return BaseRespone.error("请登录");
		}
		ChapterRecordVo info = chapterService.initChapterRecord(chapterId, accountId);
		return BaseRespone.success(info);
	}
	
	@PostMapping("/getRecordVoById")
	public BaseRespone getRecordVoById(
			@RequestParam(required = true) String recordId,
			@RequestParam(required = false) String accountId) {
		ChapterRecordVo info = chapterService.getChapterRecordVoById(recordId, accountId);
		return BaseRespone.success(info);
	}
	
	@PostMapping("/saveRecord")
	public BaseRespone saveRecord(
			@RequestParam(required = true) String recordId,
			@RequestParam(required = true) String accountId) {
		return chapterService.saveRecord(recordId, accountId);
	}
	
	@PostMapping("/getImitate")
	public 	BaseRespone getImitate(
			@RequestParam(required = false, defaultValue = "false") Boolean init,
			@RequestParam(required = true) String accountId) {
		Object user = ServletUtil.getSession().getAttribute(MyCons.USER_ROLE);
		if(null == user) {
			return BaseRespone.error("请登录");
		}
		return chapterService.getImitate(init, accountId);
	}
	
	@PostMapping("/reStartRecord")
	public BaseRespone reStartRecord(
			@RequestParam(required = true) String recordId,
			@RequestParam(required = true) String accountId) {
		return chapterService.reStartRecord(recordId, accountId);
	}
	
	@PostMapping("/reSelectRecord")
	public BaseRespone reSelectRecord(
			@RequestParam(required = true) String recordId,
			@RequestParam(required = true) String accountId) {
		return chapterService.reSelectRecord(recordId, accountId);
	}
	
	/*@PostMapping("/updateRecord")
	public BaseRespone updateRecord(
			@RequestParam(required = true) String recordId,
			@RequestParam(required = true) String accountId) {
		return chapterService.tempSaveRecord(recordId, accountId);
	}
	
	@PostMapping("/tempRecord")
	public BaseRespone tempRecord(
			@RequestBody SubmitRecord submitRecord) {
		return chapterService.recordTemp(submitRecord);
	}*/
	
	@PostMapping("/submitRecord")
	public BaseRespone submitRecord(
			@RequestParam(required = true) String recordId, 
			@RequestParam(required = true) String accountId) {
		return chapterService.recordFinish(recordId, accountId);
	}
	
	@PostMapping("/submitAnswer")
	public BaseRespone submitAnswer(
			@RequestBody SubmitRecord submitRecord) {
		return chapterService.submitAnswer(submitRecord);
	}
	
	@PostMapping("/getChapters")
	public TableRespone getChapters(
			@RequestParam(required = false, defaultValue = "") Integer type, 
			@RequestParam(required = false, defaultValue = "") String title, 
			@RequestParam(required = false, defaultValue = "") String accountId, 
			@RequestParam(required = false, defaultValue = "0") int page, 
			@RequestParam(required = false, defaultValue = "0") int size) {
		return chapterService.getChapterVos(type, title, accountId, page, size);
	}
	
	@PostMapping("/getChapter")
	public  ExChapter getChapter(
			@RequestParam(required = true) String chapterId) {
		return chapterService.getChapterInfo(chapterId);
	}
	
	@PostMapping("/getRecords")
	public TableRespone getRecords(
			@RequestParam(required = true) String type, 
			@RequestParam(required = false) String accountId, 
			@RequestParam(required = false, defaultValue = "0") int page, 
			@RequestParam(required = false, defaultValue = "0") int size) {
		return chapterService.getChapterRecords(null, type, accountId, page, size);
	}
	
	@PostMapping("/getZTTopics")
	public TableRespone getZTTopics(
			@RequestParam(required = true) String chapterId, 
			@RequestParam(required = false) String accountId, 
			@RequestParam(required = false, defaultValue = "0") int page, 
			@RequestParam(required = false, defaultValue = "0") int size) {
		return topicService.getExamTopicVos(chapterId, accountId, page, size);
	}
	
	@PostMapping("/getWrongs")
	public TableRespone getWrongs(
			@RequestParam(required = false) String recordId, 
			@RequestParam(required = true) String accountId, 
			@RequestParam(required = false, defaultValue = "") Integer result,
			@RequestParam(required = false, defaultValue = "0") int page, 
			@RequestParam(required = false, defaultValue = "0") int size) {
		return topicService.getWrongs(accountId, recordId, result, page, size);
	}
	
	@PostMapping("/saveChapter")
	public BaseRespone saveChapter(@RequestBody ExChapter chapter) {
		return chapterService.saveChapter(chapter);
	}
	
}
